package com.shiguiwu.springmybatis.spring.aop.demo;


import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.Pointcut;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;

import java.lang.reflect.Method;

/**
 * @description: 方法动态匹配
 * @author: stone
 * @date: Created by 2021/5/19 14:50
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.spring.aop.demo
 */
public class DemoV3Tests {

    public static void main(String[] args) {

        UserService target = new UserService();

        Pointcut pointcut = new PointcutV2();

        //创建通知，此处需要在方法之前执行操作，所以需要用到MethodBeforeAdvice类型的通知
        MethodBeforeAdvice advice = new MethodBeforeAdvice() {
            @Override
            public void before(Method method, Object[] objects, Object o) throws Throwable {
                System.out.println("感谢您一路的支持!");

            }
        };


        //创建Advisor，将pointcut和advice组装起来
        DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(pointcut, advice);

        //通过spring代理工具创建代理
        ProxyFactory proxyFactory = new ProxyFactory();

        //为工厂指定目标对象
        proxyFactory.setTarget(target);

        //调用addAdi 指定目标对象增强的功能
        proxyFactory.addAdvisor(advisor);

        //创建代理对象
        Object proxy = proxyFactory.getProxy();

        UserService userService = (UserService) proxy;

        System.out.println(userService);
        System.out.println(target);
        System.out.println(target== userService);

        userService.work("石贵武粉丝");

    }
}
